//# sourceURL=http://uMapApp/Module/Camera/Camera
define([
    "mhjs/mhjs"
    , "uMap/uMap"
    , "uEarth/uEarth"
    , "../../UI/Layer"
    , "../../Earth/ViewPort"
    , "../StakeNum/StakeNum"
    , "../../Earth/ViewShed"
    , "../../Earth/DataSourceView/C3DTilePrimitiveDataSourceView"
], function (
    mhjs,
    uMap,
    uEarth,
    Layer,
    ViewPort,
    StakeNum,
    ViewShed,
    C3DTilePrimitiveDataSourceView
) {

    function Camera(option) {
        var _this = this;
        option && _this.OptionToProperties(option);

        _this.stakeNum = StakeNum.transform(_this["桩号"]);

        _this.title = _this["名称"];
        _this.searchKey = _this["名称"] + " " + _this["类型"] + " " + _this["朝向"];
        _this.sortKey = _this.stakeNum && _this.stakeNum.distance;

        _this.viewport = new ViewPort($.extend(
            {},
            {center: _this.position},
            _this.viewport || {}
        ));

        _this.layer = new Layer({
            "app"     : _this.app,
            "viewport": _this.viewport
        });

        if (_this.position) {

            _this.position = new uMap.Position(_this.position);

            _this.marker = new uMap.Entity({
                objectOption: {
                    position : _this.position,
                    billboard: new uMap.BillboardGraphic({
                        objectOption: {
                            image                   : _this.app.icons.getIconMarker(_this.icon),
                            verticalOrigin          : "BOTTOM",
                            disableDepthTestDistance: Number.POSITIVE_INFINITY
                        }
                    }),
                    label    : new uMap.LabelGraphic({
                        objectOption: {
                            text                    : _this["名称"],
                            verticalOrigin          : "TOP",
                            disableDepthTestDistance: Number.POSITIVE_INFINITY
                        }
                    })
                }
            });
            _this.layer.dataSources.entities.add(_this.marker.object);

            _this.marker.on("click", function () {
                _this.trigger("click");
            });

        }

        if (_this.model) {

            _this.model.layer = _this.layer;

            _this.model = new C3DTilePrimitiveDataSourceView(_this.model);
            _this.layer.primitives.add(_this.model.tileSet.object);
        }

        if (_this.viewShed) {
            _this.viewShed.earth = _this.app.earth;
            _this.videoShed = new ViewShed(_this.viewShed);
        }



        _this.on("click", function () {
            _this.active();
            _this.openVideo();
            _this.videoShed && _this.videoShed.open();
        });

    }

    var _class = Camera;

    mhjs.OptionToPropertiesInterface(_class);
    mhjs.EventInterface(_class);

    Object.defineProperties(_class.prototype, {
        "open"      : {
            value: function () {

                var _this = this;

                if (!this.isOpened) {

                    this.layer && this.layer.open();
                    this.isOpened = true;
                    this.trigger("open");
                }
            }
        },
        "close"     : {
            value: function () {
                if (this.isOpened) {
                    this.layer && this.layer.close();
                    this.active(false);
                    this.videoShed && this.videoShed.close();

                    // this.closeVideo();
                    this.isOpened = false;
                    this.trigger("close");
                }
            }
        },
        "toggle"    : {
            value: function () {
                if (this.isOpened) {
                    this.close();
                } else {
                    this.open();
                }
            }
        },
        "active"    : {
            value: function (isActive) {
                var _this = this;

                isActive = (isActive === undefined) || Boolean(isActive);
                _this.isActive = _this.isActive === undefined ? false : _this.isActive;

                if (isActive !== _this.isActive) {

                    if (isActive) {

                        if (_this.layer) {
                            // _this.layer.active();
                            _this.marker.label.fillColor = new uMap.Color(["#ff0"]);
                            _this.marker.label.scale = 1.2;
                            _this.marker.billboard.image = _this.app.icons.getIconMarker(_this.icon, true);
                        }

                        // if (_this.videoShed) {
                        //     _this.videoShed && _this.videoShed.open();
                        // }
                        //
                        // _this.openVideo();

                        _this.trigger("active");
                    } else {

                        if (_this.layer) {
                            _this.marker.label.fillColor = new uMap.Color(["#fff"]);
                            _this.marker.label.scale = 1;
                            _this.marker.billboard.image = _this.app.icons.getIconMarker(_this.icon);
                        }

                        if (_this.videoShed) {
                            _this.videoShed && _this.videoShed.close();
                        }

                        _this.closeVideo();

                        _this.trigger("unActive");
                    }

                    _this.isActive = isActive;

                }

            }
        },
        "openVideo" : {
            value: function () {

                var _this = this;

                if (_this["视频流地址"]) {

                    _this.app.videoPlayer.title = _this.title;
                    _this.app.videoPlayer.source = _this["视频流地址"];
                    // _this.app.videoPlayer.source = "http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8";
                    _this.app.videoPlayer.open();

                    _this.app.videoPlayer.on("close", function () {
                        _this.active(false);
                        _this.app.videoPlayer.off("close");
                    });

                }
            }
        },
        "closeVideo": {
            value: function () {

                var _this = this;

                if (_this["视频流地址"]) {
                    _this.app.videoPlayer.close(false);
                }
            }
        }
    });

    return _class;

});